/**
 * 说明：系统路由相关
 * 创建人：pluto
 * 创建时间：2018/8/3
 */
angular.module('app.route',[]).config(['$stateProvider', function ($stateProvider) {
    /**
     * 根据路由，配置延迟加载的文件
     * @param modelName 模块名称，必需是pages目录里面的文件夹的名称
     * @param files 需要引用的文件
     * @returns {{_: [string,null]}}
     */
    var resolveDep = function (modelName, files) {
        var defaultFiles = [
            "pages/" + modelName + "/index.js",
            "pages/" + modelName + "/css/style.css",
            "pages/" + modelName + "/controller/controller.js",
            "pages/" + modelName + "/service/service.js"
        ];
        if (files) {
            // defaultFiles = defaultFiles.concat(files);
            defaultFiles = files;
        }
        return {
            _: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    {
                        files: defaultFiles,
                        cache: true
                    }
                ]);
            }]
        }
    };
    /**
     * 路由配置
     * @param option
     * @returns {{url: string, templateUrl: (string|string|string), controller: string, resolve: {_: (string|null)[]}, ncyBreadcrumb: {label: (string|string|string|string|string|string|*), parent: (string|string)}}}
     */
    var stateOption = function (option) {
        var modelName = option.modelName,       //模块名称，必需是pages目录里面的文件夹的名称
            routeUrl = option.routeUrl,         //路由地址
            routeName = option.routeName,       //路由的名称
            parentRoute = option.parentRoute,   //父级路由
            files = option.files,               //需要引用的文件
            tpl = option.tpl,                   //路由页面模板
            param = option.param,               //路由参数
            data = option.data;                 //路由进入前准备数据

        //modelName = modelName.toLowerCase();
        var ctrl = firstUpperCase(modelName) + "Ctrl";
        var url = "/"+ routeUrl;
        if(param){
            url += param;
        }
        tpl = tpl || 'pages/' + modelName + '/view/view.html';

        var resolve = resolveDep(modelName, files);
        if(data){
            resolve = Object.assign(resolve, data);
        }
        return {
            url: url,
            templateUrl: tpl,
            controller: ctrl,
            resolve: resolve,
            ncyBreadcrumb: {
                label: routeName,
                parent: parentRoute
            }
        }
    };
    $stateProvider
        .state('login', stateOption({
            modelName: 'login',
            routeName: '用户登录',
            routeUrl: 'login',
        }))
        .state('main', stateOption({
            modelName:'main',
            routeName: '救兵平台',
            routeUrl: 'main',
            data: {
                productTypes: ['CommonService', function (CommonService) {
                    return CommonService.listProductType().then(function (res) {
                        sessionStorage.setItem("productTypes", JSON.stringify(res));
                        return res;
                    })
                }]
            }
        }))
        .state('main.home', stateOption({
            modelName: 'home',
            routeName: '系统首页',
            routeUrl: 'home',
            parentRoute:"main"
        }))
        .state('main.auth', stateOption({
            modelName:'auth',
            routeName:'认证管理',
            routeUrl: 'auth',
            parentRoute:'main'
        }))
        .state('main.auth.factory',stateOption({
            modelName: 'factory',
            routeUrl: 'factory',
            routeName: '工厂方认证',
            tpl: 'pages/auth/view/factory.html',
            files: ['pages/auth/controller/factory.js'],
            parentRoute:"main.auth"
        }))
        .state('main.auth.business',stateOption({
            modelName: 'business',
            routeUrl: 'business',
            routeName: '订单方认证',
            tpl: 'pages/auth/view/business.html',
            files: ['pages/auth/controller/business.js'],
            parentRoute:"main.auth"
        }))
        .state('main.customer', stateOption({
            modelName:'customer',
            routeName:'客户管理',
            routeUrl: 'customer',
            parentRoute:'main'
        }))
        .state('main.customer.factory',stateOption({
            modelName: 'factoryList',
            routeUrl: 'factory',
            routeName: '工厂方客户',
            tpl: 'pages/customer/view/factory.html',
            files: ['pages/customer/controller/factory.js', 'pages/customer/service/service.js'],
            parentRoute:"main.customer"
        }))
        .state('main.customer.business',stateOption({
            modelName: 'businessList',
            routeUrl: 'business',
            routeName: '订单方认证',
            tpl: 'pages/customer/view/business.html',
            files: ['pages/customer/controller/business.js', 'pages/customer/service/service.js'],
            parentRoute:"main.customer"
        }))
        .state('main.cash',stateOption({
            modelName: 'cash',
            routeUrl: 'cash',
            routeName: '提现管理',
            parentRoute:"main"
        }))
        .state('main.commission',stateOption({
            modelName: 'commission',
            routeUrl: 'commission',
            routeName: '佣金明细',
            parentRoute:"main"
        }))
        .state('main.compeval', stateOption({
            modelName:'compeval',
            routeName:'评论投诉管理',
            routeUrl: 'compeval',
            parentRoute:'main'
        }))
        .state('main.compeval.complain', stateOption({
            modelName: 'complain',
            routeUrl: 'complain',
            routeName: '投诉管理',
            tpl: 'pages/compeval/view/complain.html',
            files: ['pages/compeval/controller/complain.js','pages/compeval/service/complain.js'],
            parentRoute:"main.compeval"
        }))
        .state('main.compeval.shaidan', stateOption({
            modelName: 'shaiDan',
            routeUrl: 'shaidan',
            routeName: '投诉管理',
            tpl: 'pages/compeval/view/shaidan.html',
            files: ['pages/compeval/controller/shaidan.js','pages/compeval/service/shaidan.js'],
            parentRoute:"main.compeval"
        }))
        .state('main.system', stateOption({
            modelName:'system',
            routeUrl: 'system',
            routeName:'运营管理',
            parentRoute:"main"
        }))
        .state('main.system.protocol', stateOption({
            modelName:'protocol',
            routeUrl: 'protocol',
            routeName:'担保交易协议',
            parentRoute:"main.system",
            files:[
                'pages/system/controller/protocol.js',
                'pages/system/service/protocol.js'
            ],
            tpl:'pages/system/view/protocol.html',
            param:"/:type"
        }))
        .state('main.system.setting', stateOption({
            modelName:"setting",
            routeUrl: 'setting',
            routeName:'运营参数设置',
            parentRoute:'main.system',
            tpl: 'pages/system/view/setting.html',
            files: [
                'pages/system/controller/setting.js',
                'pages/system/service/setting.js'
            ]
        }))
        .state('main.system.product', stateOption({
            modelName:'product',
            routeUrl: 'product',
            routeName:'产品分类管理',
            parentRoute:"main.system",
            tpl: 'pages/system/view/product.html',
            files: [
                "pages/system/css/product.css",
                'pages/system/controller/product.js',
                'pages/system/service/product.js'
            ]
        }))
        .state('main.system.ad', stateOption({
            modelName: 'ad',
            routeUrl: 'ad',
            routeName: '广告管理',
            parentRoute:"main.system",
            tpl: 'pages/system/view/ad.html',
            files: [
                "pages/system/css/product.css",
                'pages/system/controller/ad.js',
                'pages/system/service/ad.js'
            ]
        }))
        .state('main.system.call', stateOption({
            modelName: 'callList',
            routeUrl: 'call',
            routeName: '广告管理',
            parentRoute:"main.system",
            tpl: 'pages/system/view/call-list.html',
            files: [
                "pages/system/css/product.css",
                'pages/system/controller/call-list.js',
                'pages/system/service/call-list.js'
            ]
        }))
}]);
